package com.tuotu.gps.utils;

import android.content.Context;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.model.HttpHeaders;
import com.tuotu.gps.entity.LocationEntity;
import com.tuotu.gps.entity.LocationInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;


/**
 * Created by XuYeWen on 2017/12/14.
 */

public class HttpNetworkUtil {

    private static final String URL_SEND_POINT="http://coll.e-dog.cn/Api/Edog/upload_coll_place";
    private static final String URL_SEND_POINT2="http://coll.e-dog.cn/Api/Edog/test";
    private static final String URL_SEND_MP3="http://coll.e-dog.cn/Api/Edog/upload_mp3";
    private static final String URL_SEND_PATH="http://coll.e-dog.cn/Api/Edog/upload_traces";
    private static final String URL_USER_LOGIN="http://coll.e-dog.cn/Api/Edog/login";

    private  List<String> uploadList=new ArrayList<>();
    private  List<String> uploadFailList=new ArrayList<>();
    private  onResultListener onResultListener;

    public HttpNetworkUtil(){

    }

    public List<String> getUploadFailList() {
        return uploadFailList;
    }

    public List<String> getUploadList() {
        return uploadList;
    }

    public void setOnResultListener(HttpNetworkUtil.onResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }

    public interface onResultListener{
        void onSuccess(String s, String path);
        void onError(String path);
        void onSuccessInfo(String s, LocationInfo locationInfo);
        void onFailInfo(LocationInfo locationInfo);
    }

    public void setUploadFailList(List<String> uploadFailList) {
        this.uploadFailList = uploadFailList;
    }

    public void setUploadList(List<String> uploadList) {
        this.uploadList = uploadList;
    }

    //http://coll.e-dog.cn/Api/Edog/upload_coll_place
    // {"USER_ID":1234,"DATETIME":2017_12_06_08_34_45,"LON":113.02813784,"LAT":27.40579057,"DIR":182.5}
    public void httpPostPoint(Context context, String userId, String time, final double lon, final double lat, final float dir, final String path){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("USER_ID",userId);
            jsonObject.put("DATETIME",time);
            jsonObject.put("LON",lon);
            jsonObject.put("LAT",lat);
            jsonObject.put("DIR",dir);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LogUtil.print("httpSendPoint jsonobject=="+jsonObject.toString());
        OkHttpUtils.post(URL_SEND_POINT)//
                .tag(context)//
                .upString(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.print("轨迹点上传结果---"+s);
                        LogUtil.print("轨迹点上传结果---"+response.toString());
                        onResultListener.onSuccess(s,path);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
//                        Utils.createGpsFile(lon,lat,dir,path);
                        onResultListener.onError(path);
                    }
                });

    }

    public  void httpPostMp3( Context context,  String userId, final File file){
        LogUtil.print("文件上传---"+file.getName());
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("USER_ID",userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        List<File> list=new ArrayList<>();
        list.add(file);
        HttpHeaders headers=new HttpHeaders();
        headers.put("USER_ID",userId);
        OkHttpUtils.post(URL_SEND_MP3)//
                .tag(context)//
                .headers(headers)
                .addFileParams("file",list)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.print("okhttp文件上传结果---"+s);
                        LogUtil.print("okhttp文件上传结果---"+response.toString());
                        onResultListener.onSuccess(s,file.getPath());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        LogUtil.print("okhttp文件上传结果onError---"+response);
                        LogUtil.print("okhttp文件上传结果e---"+ e.toString());
                        onResultListener.onError(file.getPath());
                    }

                });

    }

    public  void httpPostMp3AndTxt(Context context, String userId, final LocationInfo locationInfo){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("USER_ID",userId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        File file1=new File(locationInfo.getMp3PathName());
        File file2=new File(locationInfo.getTxtPathName());
        List<File> list=new ArrayList<>();
        list.add(file1);
        list.add(file2);
        HttpHeaders headers=new HttpHeaders();
        headers.put("USER_ID",userId);
        OkHttpUtils.post(URL_SEND_POINT)//
                .tag(context)//
                .headers(headers)
                .params("USER_ID",userId)
                .params("upload_mp3", file1) // 可以添加文件上传
                .params("upload_coll_place", file2)
//                .addFileParams("file",list)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.print("okhttp文件上传结果---"+s);
//                        LogUtil.print("okhttp文件上传结果---"+response.toString());
                        onResultListener.onSuccessInfo(s,locationInfo);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
//                        LogUtil.print("okhttp文件上传结果onError---"+response);
//                        LogUtil.print("okhttp文件上传结果e---"+ e.toString());
                        onResultListener.onFailInfo(locationInfo);
                    }

                });

    }

    public void httpPostMp3List( Context context,  String userId, List<File> list){
        HttpHeaders headers=new HttpHeaders();
        headers.put("USER_ID",userId);
        OkHttpUtils.post(URL_SEND_MP3)//
                .tag(context)//
                .headers(headers)
                .addFileParams("file",list)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.print("okhttp list文件上传结果---"+s);
                        LogUtil.print("okhttp list文件上传结果文件上传结果---"+response.toString());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        LogUtil.print("okhttp list文件上传结果onError---"+response);
                        LogUtil.print("okhttp list文件上传结果e---"+ e.toString());
                    }

                });

    }

    public  void httpPostPath(final Context context, String userId, final ArrayList<LocationEntity> list){
        int size=list.size();
        if(size<=0)
            return;
        JSONArray array = new JSONArray();
        for (LocationEntity c : list)
            try {
                array.put(c.toJSON());
            } catch (JSONException e) {
                e.printStackTrace();
            }

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("USER_ID",userId);
            jsonObject.put("DATA",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LogUtil.print("轨迹上传---"+ jsonObject.toString());
        OkHttpUtils.post(URL_SEND_PATH).tag(context).
                upString(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s , Call call, Response response) {
                        LogUtil.print("轨迹上传结果---"+s);
                        LogUtil.print("轨迹上传结果---"+response.toString());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
    }

    public  void httpPostPath(final Context context, String userId, JSONArray array, final String path){

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("USER_ID",userId);
            jsonObject.put("DATA",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.post(URL_SEND_PATH).tag(context).
                upString(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s , Call call, Response response) {
                        LogUtil.print("轨迹上传结果---"+s);
                        LogUtil.print("轨迹上传结果---"+response.toString());
                        onResultListener.onSuccess(s,path);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        onResultListener.onError(path);
                    }
                });
    }

    public  void httpPostPathFile(final Context context, String userId, final File file){

        OkHttpUtils.post(URL_SEND_PATH)//
                .tag(context)//
                .params("USER_ID",userId)
                .params("upload_traces", file) // 可以添加文件上传
//                .params("upload_coll_place", file2)
//                .addFileParams("file",list)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s , Call call, Response response) {
                        LogUtil.print("轨迹上传结果---"+s);
//                        LogUtil.print("轨迹上传结果---"+response.toString());
                        if(file.exists()){
                            file.delete();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
//                        LogUtil.print("轨迹上传结果-call--"+ call.request().toString());
//                        LogUtil.print("轨迹上传结果-response--"+response.toString());
//                        LogUtil.print("轨迹上传结果-Exception--"+e.toString());
                    }
                });
    }

    public static void httpUserLogin(Context context, String username, String password, final HttpCallback callback){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("username",username);
            jsonObject.put("password",password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpHeaders headers=new HttpHeaders();
        headers.put("username",username);
        headers.put("password",password);
        try {
            OkHttpUtils.post(URL_USER_LOGIN)
                    .tag(context)
                    .headers(headers)
                    .params("username",username)
                    .params("password",password)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
//                            LogUtil.print("http onSuccess s==" + s);
//                            LogUtil.print("http onSuccess response==" + response.toString());
                            callback.onSuccess(s,response);
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
//                            LogUtil.print("http onError response==" + response.toString());
//                            LogUtil.print("http onError response==" + e.toString());
                            callback.onError(call,response,e);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
